﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.Oracle
{
    public class WMS_T_OutStockRowsDAL : BaseOracleDAL<WMS_T_OutStockRows>
    {
        public List<WMS_T_OutStockRows> GetAll()
        {
            var sql = @"SELECT RAWTOHEX(sys_guid()) as guid,t$orno as BillNo,t$sqnb as SN,t$pono as RowNo,t$item as ProductNumber,t$qoor as PlanQty,0 as SelectQty,0 as RealQty FROM TTDDEV111101 WHERE t$host=1 ";
            var dt = OracleDBHelper.GetDataSet(sql).Tables[0];
            var result = MakeTablePackage(dt);
            return result;
        }

        public List<WMS_T_OutStockRows> GetAllSalePlan()
        {
            var sql = @"SELECT RAWTOHEX(sys_guid()) as guid,t$worn as BillNo,t$wseq as SN,t$wpon as RowNo,t$wset as SetNo,t$item as ProductNumber,t$qoro as PlanQty,0 as SelectQty,0 as RealQty FROM TTDDEV121101 WHERE t$host=1 ";
            var dt = OracleDBHelper.GetDataSet(sql).Tables[0];
            var result = MakeTablePackage(dt);
            return result;
        }
        /// <summary>
        /// 更新中间表同步字段状态
        /// </summary>
        /// <param name="list"></param>
        public int UpdateHost(List<WMS_T_OutStockRows> list)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("begin");
            foreach (var item in list)
            {
                var sql = "UPDATE TTDDEV111101 set  t$host=2,t$wdte1=SYSDATE where  t$pono='" + item.RowNo + "' and t$orno='" + item.BillNo + "' and t$sqnb='" + item.SN + "';";
                sb.AppendLine(sql);
            }
            sb.AppendLine("end;");

            var resultSql = sb.ToString().Trim();

            var result = OracleDBHelper.ExecuteNonQuery(resultSql);

            return result;
        }


        public int UpdateHostSalePlan(List<WMS_T_OutStockRows> list)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("begin");
            foreach (var item in list)
            {
                var sql = "UPDATE TTDDEV121101 set  t$host=2,t$wdte1=SYSDATE where  t$wpon='" + item.RowNo + "' and t$worn='" + item.BillNo + "' and t$wseq='" + item.SN + "' and t$wset='"+ item.SetNo + "' ;";
                sb.AppendLine(sql);
            }
            sb.AppendLine("end;");

            var resultSql = sb.ToString().Trim();

            var result = OracleDBHelper.ExecuteNonQuery(resultSql);

            return result;
        }
    }
}
